package com.ruoyi.riskAssessment.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.riskAssessment.dto.RiskLedgerDto;
import com.ruoyi.riskAssessment.vo.RiskLedgerVo;
import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.mapper.SysDictItemMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.riskAssessment.mapper.RiskLedgerMapper;
import com.ruoyi.riskAssessment.domain.RiskLedger;
import com.ruoyi.riskAssessment.service.IRiskLedgerService;

/**
 * 危险源-风险台账Service业务层处理
 *
 * @author lth
 * @date 2023-12-15
 */
@Service
public class RiskLedgerServiceImpl implements IRiskLedgerService
{
    @Autowired
    private RiskLedgerMapper riskLedgerMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 查询危险源-风险台账
     *
     * @param riskLedgerId 危险源-风险台账主键
     * @return 危险源-风险台账
     */
    @Override
    public RiskLedger selectRiskLedgerByRiskLedgerId(String riskLedgerId)
    {
        return riskLedgerMapper.selectRiskLedgerByRiskLedgerId(riskLedgerId);
    }

    /**
     * 查询危险源-风险台账列表
     *
     * @param riskLedger 危险源-风险台账
     * @return 危险源-风险台账
     */
    @Override
    public List<RiskLedger> selectRiskLedgerList(RiskLedger riskLedger)
    {
        return riskLedgerMapper.selectRiskLedgerList(riskLedger);
    }

    /**
     * 新增危险源-风险台账
     *
     * @param riskLedger 危险源-风险台账
     * @return 结果
     */
    @Override
    public int insertRiskLedger(RiskLedger riskLedger)
    {
        return riskLedgerMapper.insertRiskLedger(riskLedger);
    }

    /**
     * 修改危险源-风险台账
     *
     * @param riskLedger 危险源-风险台账
     * @return 结果
     */
    @Override
    public int updateRiskLedger(RiskLedger riskLedger)
    {
        return riskLedgerMapper.updateRiskLedger(riskLedger);
    }

    /**
     * 批量删除危险源-风险台账
     *
     * @param riskLedgerIds 需要删除的危险源-风险台账主键
     * @return 结果
     */
    @Override
    public int deleteRiskLedgerByRiskLedgerIds(String[] riskLedgerIds)
    {
        return riskLedgerMapper.deleteRiskLedgerByRiskLedgerIds(riskLedgerIds);
    }

    /**
     * 删除危险源-风险台账信息
     *
     * @param riskLedgerId 危险源-风险台账主键
     * @return 结果
     */
    @Override
    public int deleteRiskLedgerByRiskLedgerId(String riskLedgerId)
    {
        return riskLedgerMapper.deleteRiskLedgerByRiskLedgerId(riskLedgerId);
    }

    /**     矿台总账列表
    * @Author lth
    * @Description //TODO
    * @Date 16:24 2024/1/4
    * @Param
    * @return
     **/
    @Override
    public PageInfo<RiskLedgerDto> ledgerList(RiskLedgerVo vo) {
        List<RiskLedger> riskLedgerList = riskLedgerMapper.selectRiskLedgerList(vo);
        PageInfo info = new PageInfo<>(riskLedgerList);
        List<RiskLedgerDto> dtoList = riskLedgerList.stream().map(riskLedger -> {
            RiskLedgerDto riskLedgerDto = new RiskLedgerDto();
            BeanUtils.copyProperties(riskLedger, riskLedgerDto);

            String riskSourceTypeId = riskLedger.getRiskSourceTypeId();
            if (riskSourceTypeId != null) {
                SysDictItem riskType = sysDictItemMapper.getRiskType(riskLedger.getRiskSourceTypeId());
                if (riskType != null) {
                    riskLedgerDto.setRiskSourceType(riskType.getItemText());
                }
            }
            String professionalTypeId = riskLedger.getProfessionalTypeId();
            if (professionalTypeId != null) {
                SysDictItem professional = sysDictItemMapper.getProfessional(riskLedger.getProfessionalTypeId());
                if (professional != null) {
                    riskLedgerDto.setProfessionalType(professional.getItemText());
                }
            }
            String scheduleTaskId = riskLedger.getScheduleTaskId();
            if (scheduleTaskId != null) {
                SysDictItem scheduleTaskByScheduleTaskId = sysDictItemMapper.getScheduleTaskByScheduleTaskId(riskLedger.getScheduleTaskId());
                if (scheduleTaskByScheduleTaskId != null) {
                    riskLedgerDto.setScheduleTask(scheduleTaskByScheduleTaskId.getItemText());
                }
            }
            String manageStatus = riskLedger.getManageStatus();
            if (manageStatus != null) {
                SysDictItem status = sysDictItemMapper.getManageStatus(manageStatus);
                if (status != null) {
                    riskLedgerDto.setManageStatus(status.getItemText());
                }
            }
            return riskLedgerDto;
        }).collect(Collectors.toList());
        info.setList(dtoList);

        return info;
    }

    /**     个人台账列表
    * @Author lth
    * @Description //TODO
    * @Date 11:20 2024/1/18
    * @Param
    * @return
     **/
    @Override
    public PageInfo<RiskLedgerDto> personLedgerList(RiskLedgerVo vo) {
        List<RiskLedger> riskLedgerList = riskLedgerMapper.personLedgerList();

        PageInfo info = new PageInfo<>(riskLedgerList);
        List<RiskLedgerDto> dtoList = riskLedgerList.stream().map(riskLedger -> {
            RiskLedgerDto riskLedgerDto = new RiskLedgerDto();
            BeanUtils.copyProperties(riskLedger, riskLedgerDto);

            String riskSourceTypeId = riskLedger.getRiskSourceTypeId();
            if (riskSourceTypeId != null) {
                SysDictItem riskType = sysDictItemMapper.getRiskType(riskLedger.getRiskSourceTypeId());
                if (riskType != null) {
                    riskLedgerDto.setRiskSourceType(riskType.getItemText());
                }
            }
            String professionalTypeId = riskLedger.getProfessionalTypeId();
            if (professionalTypeId != null) {
                SysDictItem professional = sysDictItemMapper.getProfessional(riskLedger.getProfessionalTypeId());
                if (professional != null) {
                    riskLedgerDto.setProfessionalType(professional.getItemText());
                }
            }
            String scheduleTaskId = riskLedger.getScheduleTaskId();
            if (scheduleTaskId != null) {
                SysDictItem scheduleTaskByScheduleTaskId = sysDictItemMapper.getScheduleTaskByScheduleTaskId(riskLedger.getScheduleTaskId());
                if (scheduleTaskByScheduleTaskId != null) {
                    riskLedgerDto.setScheduleTask(scheduleTaskByScheduleTaskId.getItemText());
                }
            }
            String manageStatus = riskLedger.getManageStatus();
            if (manageStatus != null) {
                SysDictItem status = sysDictItemMapper.getManageStatus(manageStatus);
                if (status != null) {
                    riskLedgerDto.setManageStatus(status.getItemText());
                }
            }
            return riskLedgerDto;
        }).collect(Collectors.toList());
        info.setList(dtoList);

        return info;

       // return dtoList;
    }
}
